package com.crawler.dao;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import com.crawler.util.DruidUtil;

/**   
 * @ClassName:     DButils   
 * @Description:   保存数据 
 * @author:        wl
 * @date:          2017年9月12日 下午2:27:21     
 */  
public class DButils {
	
	
	public static Connection getConnect(){
		Connection conn = null;
//		try {
//			Class.forName("com.mysql.jdbc.Driver");
//			conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/book3?useUnicode=true&characterEncoding=utf-8&useSSL=true", "root", "123456");
//		} catch (Exception e) {
//			e.printStackTrace();
//		};
		
		
		//c3p0数据
//		try {
//			conn= JDBCUtils.getInstence().getConnection();
//		} catch (SQLException e) {
//			e.printStackTrace();
//		}
		try {
			conn = DruidUtil.getDataSource().getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}

	
	public static Integer insertBoookName(String name,String author){
		Connection conn = getConnect();
		Integer id = null;
		try {
			QueryRunner qr=new QueryRunner();
			ResultSetHandler<Integer> res = new MyResultSetHandler();
			String sql = "INSERT INTO books(name, author,date) VALUES(?,?,?)";
			Object params[] = {name,author, new Date()};
			qr.insert(conn, sql, new ScalarHandler<>(), params);
			
//			String query = "SELECT MAX(id) FROM books";
//			id = qr.query(conn, query, res);
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				conn.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return id;
	}

	public static void insertContent(Integer book_id,String chapter,String content,String sort){
		try {
			Connection conn = getConnect();
			QueryRunner qr=new QueryRunner();
			ResultSetHandler<Integer> res = new ScalarHandler<Integer>(1);	
			String sql = "INSERT INTO contents(book_id, chapter,content,sort) VALUES(?,?,?,?)";
			Object params[] = {book_id,chapter, content,sort};	
			qr.insert(conn, sql, res, params);
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}		
	}
	public static void main(String[] args) {
		long start = System.currentTimeMillis();
		CountDownLatch latch = new CountDownLatch(10000);
		ExecutorService executorService = Executors.newFixedThreadPool(32);
		for (int i = 0; i < 10000; i++) {
//			insertBoookName("测试11","测试作者11");
			Book book = new Book();
			book.setLatch(latch);
			executorService.execute(book);
		}
		try {
			latch.await();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}finally{
			executorService.shutdown();
		}
		long end = System.currentTimeMillis();
		System.out.println("耗时:"+(new BigDecimal(end-start)).divide(new BigDecimal(1000L)));
	}
	
}
class Book implements Runnable{
	private CountDownLatch latch;
	
	@Override
	public void run() {
		DButils.insertBoookName("测试11","测试作者11");
		latch.countDown();
	}
	
	public CountDownLatch getLatch() {
		return latch;
	}
	
	public void setLatch(CountDownLatch latch) {
		this.latch = latch;
	}
}
